﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class UploadDocument : PageBaseClass
{
    private MyDMSEntity myDmsEntity = new MyDMSEntity();
    private int _task_code = 0;
    private int _proj_code = 0;
    private int _dept_code = 0;
    private int _folder_code = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["task"] != null)
        {
            _task_code = int.Parse(Request.QueryString["task"]);
            var firstOrDefault = myDmsEntity.Task.FirstOrDefault(t => t.Task_Code == _task_code);
            if (firstOrDefault != null)
                txtTaskTitle.Text = firstOrDefault.Task_Title;
        }
        if (Request.QueryString["proj"] != null)
        {
            _proj_code = int.Parse(Request.QueryString["proj"]);
        }
        if (Request.QueryString["dept"] != null)
        {
            _dept_code = int.Parse(Request.QueryString["dept"]);
        }
        if (Request.QueryString["folder"] != null)
        {
            _folder_code = int.Parse(Request.QueryString["folder"]);
            var firstOrDefault = myDmsEntity.Folder.FirstOrDefault(t => t.Folder_Code == _folder_code);
            if (firstOrDefault != null)
                txtFolderTitle.Text = firstOrDefault.Folder_Title;
        }
        List<DocumentType> docTypes = myDmsEntity.DocumentType.Where(r => r.Office_Code == Author.office_Code).ToList();

        ddlDocumentTypes.DataSource = docTypes;
        ddlDocumentTypes.DataTextField = "Type_Name";
        ddlDocumentTypes.DataValueField = "DT_Code";
        ddlDocumentTypes.DataBind();

        txtRecDate.Text = DateTime.Now.ToString("MMM d, yyyy");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        Document uDoc = new Document();
        string filePath = string.Empty;
        uDoc.Document_Name = txtDocName.Text;
        uDoc.Document_Desc = txtDocDesc.Text;

        if (_task_code != 0)
        {
            uDoc.Task_Code = _task_code;
            filePath =
                Server.MapPath(string.Format("Upload/UserData/Files/{0}/{1}/{2}/", _dept_code, _proj_code,
                                             _task_code));
        }
        if (_folder_code != 0)
        {
            uDoc.Folder_Code = _folder_code;
            filePath =
                Server.MapPath(string.Format("Upload/UserData/Files/{0}/{1}/{2}/{3}/", _dept_code, _proj_code, _task_code,
                                             _folder_code));
        }

        if (!System.IO.Directory.Exists(filePath))
        {
            System.IO.Directory.CreateDirectory(filePath);
        }  
        filePath += fluDocument.FileName;
        fluDocument.SaveAs(filePath);

        uDoc.File_Path = filePath;
        uDoc.Recieved_Date = DateTime.Parse(txtRecDate.Text);
        uDoc.Hard_Location = txtLocation.Text;

        uDoc.Document_Type = int.Parse(ddlDocumentTypes.SelectedValue);
        uDoc.Owner = Author.user_Code;
        uDoc.Office_Code = Author.office_Code;

        uDoc.Creation_Date = DateTime.Now;
        uDoc.Updation_Date = DateTime.Now;
        uDoc.Is_Active = true;

        myDmsEntity.Document.AddObject(uDoc);

        if (myDmsEntity.SaveChanges() > 0)
        {
            //更新分项
            if(_folder_code!=0)
            {
                Folder folder = myDmsEntity.Folder.FirstOrDefault(f => f.Folder_Code == _folder_code);
                folder.Updation_Date = DateTime.Now;
            if (myDmsEntity.SaveChanges() <= 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ee",
                                                    "alert('文件已保存，更新分项时出错'');location.reload(true);", true);
            }
            }

            //更新小项 
            Task task = myDmsEntity.Task.FirstOrDefault(r => r.Task_Code == _task_code);
            task.Updation_Date = DateTime.Now;
            if (myDmsEntity.SaveChanges() <= 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ee",
                                                    "alert('文件已保存，更新小项时出错'');location.reload(true);", true);
            }

            //更新中项
            Project proj = myDmsEntity.Project.FirstOrDefault(p => p.Project_Code == _proj_code);
            proj.Updation_Date = DateTime.Now;
            if (myDmsEntity.SaveChanges() < 0)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "ee",
                                                    "alert('文件已保存，更新中项时出错');location.reload(true);", true);
            }

            ScriptManager.RegisterStartupScript(this, this.GetType(), "ee", "alert('文件已成功保存');location.reload(true);",
                                                true);
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ee", "alert('文件保存失败');location.reload(true);",
                                                true);
        }
    }

}
